package com.leessy.r229.server.http

import com.blankj.utilcode.util.TimeUtils
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @author : Created by liucheng. on 2021/01/04
 * @description : setRequestHeader
 */
class HeaderInterceptor : Interceptor {

    /**
     * 默认请求头数据配置
     */
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        //        String url = request.url().toString();
        val builder = request.newBuilder()
            .header("user-agent", "HPT-Machine")
            //TODO 为什么要用gb2312 body数据转码好像没有用此格式
//            .header("content-type", "application/json;charset=gb2312")
//            .header("connect", "close")
//            .header(
//                "password",
//                "${if (Config.httpPasssword2.isNullOrEmpty()) Config.httpPasssword else Config.httpPasssword2}"
//            )
//            .header("client-id", "${Config.deviceNumber}")
            .header("DevTime", "${TimeUtils.getNowString()}")
            .header("sn", LoadTask.creatRequesSerialNo())/*非必要*/

        val method = request.method()
        if (method == "POST") {
            val url = request.url()
            builder.url(url.toString() + "?sn=" + LoadTask.creatRequesSerialNo())
        }
        return chain.proceed(builder.build())
    }

}